package co.edu.ucatolica.sislab;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import co.edu.co.ucatolica.salas.domain.SalaConfiguracionBean;
import co.edu.ucatolica.sislab.domainlist.DomainListContants;
import co.edu.ucatolica.sislab.domainlist.DomainListService;
import co.edu.ucatolica.sislab.domainlist.domain.DomainListItemBean;
import co.edu.ucatolica.sislab.equipos.EquiposService;
import co.edu.ucatolica.sislab.equipos.domain.EquipoBean;
import co.edu.ucatolica.sislab.persistence.DatabaseException;
import co.edu.ucatolica.sislab.persistence.DatabasePersistorHelper;
import co.edu.ucatolica.sislab.persistence.utils.UtilsDataBaseExceptions;
import co.edu.ucatolica.sislab.salas.SalasService;
import co.edu.ucatolica.sislab.sancion.SancionService;
import co.edu.ucatolica.sislab.sanciones.domain.SancionBean;
import co.edu.ucatolica.sislab.users.UserService;
import co.edu.ucatolica.sislab.users.domain.UserBean;
import co.edu.ucatolica.sislab.users.domain.UserDataBean;


public class SISLABService implements ISISLABService{
	private static SISLABService sislabService = new SISLABService();
	private SISLABService(){
	}
	public static SISLABService getInstance() {
		return sislabService;
	}
	/**
	 * Logger asociado
	 */
	private static Logger logger = LoggerFactory.getLogger(SISLABService.class);
	@Override
	public Long autenticateUser(UserBean userBean) throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		
		try{
			Long quantity = UserService.autenticateUser(transactionNumber, userBean);
			DatabasePersistorHelper.commit(transactionNumber);
			return quantity;
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}
		
		return null;
	}
	@Override
	public UserDataBean getUserDataBeanByCode(UserDataBean userDataBean)throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		
		try{
			return UserService.getUserDataBeanByCode(transactionNumber, userDataBean);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public List<DomainListItemBean> getMotivos() throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		
		try{
			return DomainListService.getDomainList(transactionNumber, DomainListContants.MOTIVOS);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public List<DomainListItemBean> getCastigos()throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		
		try{
			return DomainListService.getDomainList(transactionNumber, DomainListContants.CASTIGOS);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public List<DomainListItemBean> getTiposIdentificacion() throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		
		try{
			return DomainListService.getDomainList(transactionNumber, DomainListContants.TIPOS_IDENTIFICACION);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	public List<DomainListItemBean> getEstados() throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			return DomainListService.getDomainList(transactionNumber, DomainListContants.ESTADOS);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public Long createUser(UserBean userBean) throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			Long id = UserService.createUser(transactionNumber, userBean);
			DatabasePersistorHelper.commit(transactionNumber);
			return id;
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public Long createSancion(SancionBean sancionBean) throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			Long id = SancionService.createSancion(transactionNumber, sancionBean);
			DatabasePersistorHelper.commit(transactionNumber);
			return id;
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public List<EquipoBean> getEquiposByIdSala(Long idSala) throws DatabaseException {
		
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			return EquiposService.getEquiposByIdSala(transactionNumber, idSala);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public List<EquipoBean> getEquipos() throws DatabaseException {
		
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			return EquiposService.getEquipos(transactionNumber);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public List<SalaConfiguracionBean> getSalaConfigurationList(Long salaId) throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			return SalasService.getSalaConfigurationList(transactionNumber, salaId);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}
	@Override
	public EquipoBean getEquipoById(Long idEquipo) throws DatabaseException {
		Long transactionNumber = DatabasePersistorHelper.getNextTransaction();
		try{
			return EquiposService.getEquipoById(transactionNumber, idEquipo);
		}catch ( DatabaseException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}catch( RuntimeException e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
			throw e;						
		}catch( Exception e ) {
			UtilsDataBaseExceptions.generateDatabaseRollBack(logger, transactionNumber, e);
		}finally {
			DatabasePersistorHelper.closeConnection(transactionNumber);
			UtilsDataBaseExceptions.closeDatabaseConnection(logger, transactionNumber);
		}	
		return null;
	}	
	
	
	
}
